<template>
  <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属小区" prop="communityName">
          <el-input v-model="form.communityName" disabled/>
        </el-form-item>
        <el-form-item label="所属楼栋" prop="floorName">
          <el-input v-model="form.floorName" disabled/>
        </el-form-item>
        <el-form-item label="所属单元" prop="unitNum">
          <el-input v-model="form.unitName" disabled/>
        </el-form-item>
        <el-form-item label="单元层数" prop="layerNum">
          <el-input v-model="form.layerNum"/>
        </el-form-item>
        <el-form-item label="单元户数" prop="roomNum">
          <el-input v-model="form.roomNum"/>
        </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm"  v-hasPermi="['property:room:addLayerNum']">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addLayerNum } from "@/api/property/room";
  import { getUnit } from "@/api/property/unit";

  export default {
    name: "roomLayerForm",
    dicts: ['room_rule'],
    data() {
      return {
        open:false,
        title:'新增楼层',
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          layerNum: [
            {required: true, message: "层数不能为空", trigger: "blur"},
            {
              pattern: /^([1-9]\d*.?|0.)\d*$/,
              message: "请输入整数",
              trigger: "blur"
            }
          ],
          roomNum: [
            {required: true, message: "房间数不能为空", trigger: "blur"},
            {
              pattern: /^([1-9]\d*.?|0.)\d*$/,
              message: "请输入整数",
              trigger: "blur"
            }
          ],
        },
      };
    },
    created() {

    },
    methods: {
      show(options) {
        this.reset();
        getUnit(options.currentNode.id).then(res=>{
          this.form.communityName = res.data.communityName
          this.form.floorName = res.data.floorName
          this.form.unitName = res.data.unitName
          this.form.unitId = res.data.id
          this.open = true
        })
      },
      // 取消按钮
      cancel() {
        this.reset();
        this.open = false
      },
      // 表单重置
      reset() {
        this.form = {
          communityName:null,
          floorName: null,
          unitId: null,
          unitName:null,
          layerNum: null,
          roomNum: null
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            addLayerNum(this.form).then(response => {
              this.msgSuccess("新增楼层成功");
              this.open = false;
              this.$emit("ok")
            });
          }
        });
      }
    },
  };
</script>
